package cn.yitun.chat.yitunserverrunsingleboot.chat.common.Initializer;

import cn.yitun.chat.yitunchatbasesdk.server.im.dispatcher.handler.SessionHandler;
import cn.yitun.chat.yitunchatbasesdk.server.im.dispatcher.handler.SessionMessageHandler;
import cn.yitun.chat.yitunchatnettysdk.netty.tcp.SocketServer;
import cn.yitun.chat.yitunchatnettysdk.netty.web.WebSocketServer;
import cn.yitun.chat.yitungeneral.annotation.socket.Server;
import cn.yitun.chat.yitunservercommonlib.constant.ServerConstant;
import cn.yitun.chat.yitunserverrunsingleboot.chat.common.box.ConfigBox;
import cn.yitun.chat.yitunserverrunsingleboot.chat.common.service.InitializerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.Resource;

/**
 *
 * 容器启动后要初始化执行的操作
 *
 */
@Configuration
public class AppInitializer implements ApplicationListener<ContextRefreshedEvent> {

	protected final Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private InitializerService initializerService;
	@Resource
	private SessionMessageHandler messageHandler;
	@Resource
	private SessionHandler sessionHandler;


	@Override
	public void onApplicationEvent(ContextRefreshedEvent e) {
		if (e.getApplicationContext().getParent() == null) {
			if (logger.isInfoEnabled()) {
				logger.info(this.getClass() + "：系统初始化。");
			}
			initializerService.initialize();
			init();
		}
	}

	void init() {

		String category = "";
		String key = "";

		category = ServerConstant.ServerAddressOrPort.CATEGORY_CONFIG_CHAT.value();
//		category = "server.config.chat";

		/** 聊天服务启动TCP端口 **/
//		key = "server.config.chat.tcp.port";
		key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_TCP_PORT.value();
		String tcpPortValue = ConfigBox.get(category, key);

		/** 聊天服务WebSocket端口 **/
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_WEBSOCKET_PORT.value();
//		key = "server.config.chat.websocket.port";
		String websocketPortValue = ConfigBox.get(category, key);

		/** 聊天服务WebSocket路径 **/
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_WEBSOCKET_PATH.value();
//		key = "server.config.chat.websocket.path";
		String websocketPath = ConfigBox.get(category, key);

		int tcpPort = Integer.parseInt(tcpPortValue);
		int websocketPort = Integer.parseInt(websocketPortValue);

		SocketServer socketServer = new SocketServer(messageHandler, sessionHandler);
		WebSocketServer webSocketServer = new WebSocketServer(websocketPath, messageHandler, sessionHandler);

		socketServer.setPort(tcpPort);
		webSocketServer.setPort(websocketPort);

		new StartThread(socketServer).start();
		new StartThread(webSocketServer).start();
	}

	static class StartThread extends Thread {

		private Server server;

		public StartThread(Server server) {
			this.server = server;
		}

		public void run() {
			server.start();
		}
	}
}
